package com.yijiu.trb;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextWatcher;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.RequestAccountInfoResponse;
import com.yijiu.response.RequestPersonalInfoResponse;
import com.yijiu.utils.ConvertUtils;
import com.yijiu.utils.DialogUtils;
import com.yijiu.utils.MoneyCheck;

/**
 * 划入
 * 
 * @author TangChao
 */
public class CutIn extends BTitleActivity implements OnClickListener
{
	private EditText moneyEditText;

	private TextView bigTextView;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.RGBA_8888);
		setContentView(R.layout.cut_in_layout);
		requestAccountInfo(CacheData.APP_KEY);
	}

	@Override
	protected void initWidget()
	{
		titleView.setText("快捷划入");
		LinearLayout linearLayout = (LinearLayout) findViewById(R.id.cut_in_linear_layout);
		TextView noCardTextView = (TextView) findViewById(R.id.cut_in_card_no_card);
		noCardTextView.setOnClickListener(this);
		String bankNumStr = CacheData.BANK_CARD_NUMBER;
		if (bankNumStr.equals("null"))
		{
			linearLayout.setVisibility(View.GONE);
			noCardTextView.setVisibility(View.VISIBLE);
		}
		else
		{
			linearLayout.setVisibility(View.VISIBLE);
			noCardTextView.setVisibility(View.GONE);
		}
		/*----------------------------------------------------------------------------*/
		TextView bankNameTextView = (TextView) findViewById(R.id.tv_money_in_bank_card_name);
		TextView bankInfoTextView = (TextView) findViewById(R.id.tv_money_in_bank_card_info);
		ImageView bankIconImageView = (ImageView) findViewById(R.id.iv_money_in_bank_card_icon);
		moneyEditText = (EditText) findViewById(R.id.et_money_in_small_count);
		bigTextView = (TextView) findViewById(R.id.tv_money_in_big_count);
		moneyEditText.addTextChangedListener(new TextWatcher()
		{
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count)
			{
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after)
			{
			}

			@Override
			public void afterTextChanged(Editable s)
			{
				MoneyCheck mc = new MoneyCheck();
				bigTextView.setText(mc.toBig(s.toString()));
			}
		});
		bankNameTextView.setText(CacheData.BANK_CARD_NAME);
		String numStr = CacheData.BANK_CARD_NUMBER;
		if (numStr.equals(""))
		{
			Intent intent = new Intent();
			intent.setClass(this, MyBankCardActivity.class);
			startActivity(intent);
		}
		else
		{
			numStr = numStr.substring(numStr.length() - 4, numStr.length());
			bankInfoTextView.setText("尾号" + numStr + "储蓄卡");
		}
		ConvertUtils.checkBankIcon(CacheData.BANK_CARD_NAME, bankIconImageView);
		SpannableString sp = new SpannableString("我已阅读并同意《快捷划入代扣授权委托书》");
		TextView agreeTextView = (TextView) findViewById(R.id.tv_money_in_info);
		sp.setSpan(new CheckAgreeText(""), 8, 19, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		sp.setSpan(new ForegroundColorSpan(Color.BLACK), 8, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		agreeTextView.setText(sp);
		agreeTextView.setMovementMethod(LinkMovementMethod.getInstance());
		CheckBox isAgreeCheckBox = (CheckBox) findViewById(R.id.cb_money_in_isagree);
		final Button btnMoneyIn = (Button) findViewById(R.id.btn_money_in_submit);
		btnMoneyIn.setOnClickListener(this);
		isAgreeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if (isChecked)
				{
					btnMoneyIn.setEnabled(true);
					btnMoneyIn.setBackgroundResource(R.drawable.login_background);
				}
				else
				{
					btnMoneyIn.setEnabled(false);
					btnMoneyIn.setBackgroundResource(R.drawable.btn_login_bg_false);
				}
			}
		});
	}

	/**
	 * 账户信息请求
	 * 
	 * @param appKey
	 */
	private void requestAccountInfo(String appKey)
	{
		String inputStr = "service=queryAcountInfo";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestAccountInfoResponse requestAccountInfoResponse = new RequestAccountInfoResponse();
		Request request = new Request(action, requestAccountInfoResponse, ActionType.REQUEST_ACCOUNT_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取数据");
	}

	/**
	 * 个人信息请求
	 * 
	 * @param appKey
	 */
	private void requestPersonInfo(String appKey)
	{
		String inputStr = "service=personalInfoService";
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction action = new BaseAction(encodedData, encriptData, appKey);
		RequestPersonalInfoResponse requestPersonalInfo = new RequestPersonalInfoResponse();
		Request request = new Request(action, requestPersonalInfo, ActionType.REQUEST_PERSONAL_INFO);
		netManager.excute(request, this, this);
		showProgress("正在获取数据");
	}

	/**
	 * 请求成功
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		switch (r.getType())
		{
			case ActionType.REQUEST_ACCOUNT_INFO:// 请求账户信息
				RequestAccountInfoResponse itemAccountInfoResponse = (RequestAccountInfoResponse) r.getResponse();
				CacheData.AVAILABLE_BALANCE = itemAccountInfoResponse.field.availableBalance + "";
				requestPersonInfo(CacheData.APP_KEY);
				break;
			case ActionType.REQUEST_PERSONAL_INFO:// 请求个人信息
				RequestPersonalInfoResponse itemPersonalInfoResponse = (RequestPersonalInfoResponse) r.getResponse();
				CacheData.BANK_CARD_NAME = itemPersonalInfoResponse.field.bankName;
				CacheData.BANK_CARD_NUMBER = itemPersonalInfoResponse.field.bankCardNo;
				CacheData.BANK_PEOPLE_NAME = itemPersonalInfoResponse.field.bankOpenName;
				CacheData.REAL_NAME = itemPersonalInfoResponse.field.realName;
				initWidget();
				break;
			default:
				break;
		}
	}

	/**
	 * 请求失败
	 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.REQUEST_ACCOUNT_INFO:
			case ActionType.REQUEST_PERSONAL_INFO:
				app.showMsg(errorMsg);
				break;
			default:
				break;
		}
	}

	/**
	 * 重新请求
	 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}

	class CheckAgreeText extends URLSpan
	{
		public CheckAgreeText(String src)
		{
			super(src);
		}

		@Override
		public void onClick(View widget)
		{
			DialogUtils.showAgreeTxtDialog(CutIn.this, R.string.txt2);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == 0)
		{
			Intent intent = getIntent();
			setResult(RESULT_OK, intent);
			finish();
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
			case R.id.btn_money_in_submit:
				String moneyStr = moneyEditText.getText().toString();
				if (StringUtils.isNull(moneyStr))
				{
					app.showMsg("请输入金额");
					return;
				}
				initIntent(this, DealTextViewDialog.class, "monetStr", moneyStr);
				break;
			case R.id.cut_in_card_no_card:
				startActivityForResult(new Intent(this, AddBankCardOther.class), 0);
				break;
			default:
				break;
		}
	}
}
